/*
 * @Description:
 * @Author: zoeblow
 * @Email: wangfuyuan@nnuo.com
 * @Date: 2019-12-16 15:01:46
 * @LastEditors  : zoeblow
 * @LastEditTime : 2020-01-06 09:45:46
 * @FilePath: \monitor\controllers\up\index.js
 */
const Utils = require("../../utils");
const keyMap = require("../../sdk/cut/keyMap.js");
// const ResourceLoadModel = require("../../modules/upLog");
class Up {
  /**
   * @description: 接受并分类处理上传的日志
   * @param {object} ctx 上下文
   * @return:
   */
  static async upLog(req, res) {
    try {
      // 获取IP地址
      const clientIpString =
        req.headers["x-forwarded-for"] ||
        req.connection.remoteAddress ||
        req.socket.remoteAddress ||
        req.connection.socket.remoteAddress;
      const paramStr = req.body.data;
      // console.log(paramStr);

      const param = JSON.parse(paramStr);
      const logArray = param.logInfo.split("￥￥￥");
      for (var i = 0; i < logArray.length; i++) {
        if (!logArray[i]) continue;
        const logInfo = JSON.parse(logArray[i]);
        // console.log(logInfo, "logInfo");

        if (!logInfo.uploadType) {
          for (let key in logInfo) {
            if (keyMap[key]) {
              logInfo[keyMap[key]] = logInfo[key];
              delete logInfo[key];
            }
          }
        }
        console.log(logInfo.uploadType);

        logInfo.monitorIp = clientIpString;
        logInfo.happenTime = logInfo.happenTime + "";
        logInfo.currentURL = Utils.b64DecodeUnicode(logInfo.currentURL);
        switch (logInfo.uploadType) {
          case "RESOURCE_LOAD":
            // logInfo.sourceUrl = Utils.b64DecodeUnicode(logInfo.sourceUrl);
            console.log(logInfo, "RESOURCE_LOAD");
            // await ResourceLoadModel.createResourceLoad(logInfo);
            break;
          case "CUSTOMER_PV":
            console.log("CUSTOMER_PV", logInfo);
            break;
          case "LOAD_PAGE":
            console.log("LOAD_PAGE", logInfo);
            break;
          case "JS_ERROR":
            console.log("JS_ERROR", logInfo);
            break;
          case "HTTP_LOG":
            console.log("HTTP_LOG", logInfo);
            break;
          case "ELE_BEHAVIOR":
            console.log("ELE_BEHAVIOR", logInfo);
            break;
          case "CUSTOMIZE_BEHAVIOR":
            console.log("CUSTOMIZE_BEHAVIOR", logInfo);
            break;
          default:
            break;
        }
      }

      const obj = {
        code: 200,
        message: "日志上报成功"
      };
      res.send(obj);
    } catch (error) {
      console.error(error);
    }
  }
}

//exports//
module.exports = Up;
//exports//
